<template>
  <div class="homeNote" v-if="homeNote.topics.length > 0">
    <div class="noteTopicBox">
      <div
        class="noteTopics"
        v-for="(topic, index) in homeNote.topics"
        :key="index"
      >
        <div
          class="noteTopic"
          v-for="t in topic"
          :key="t.id"
          :class="{ isLast: t.icon == '' }"
        >
          <div class="icon" v-if="t.icon">
            <van-image width="12" height="auto" :src="t.icon" lazy-load />
          </div>
          <div class="name">
            {{ t.name }}
          </div>
        </div>
      </div>
    </div>
    <div v-masonry class="noteLists">
      <div
        v-masonry-tile
        class="noteList"
        v-for="(li, index) in homeNote.list"
        :key="index"
        transition-duration="0.3s"
      >
        <div
          class="list"
          v-if="li.type == 2"
          style="margin-top: 10px"
          @click="toActivity(li.activity.action_url)"
        >
          <img :src="li.activity.image" alt="" />
          <div class="title">{{ li.activity.name }}</div>
          <div class="bottom">
            <div class="left">
              <div class="avatar">
                <van-image
                  width="20px"
                  height="20px"
                  round
                  :src="li.activity.author.p"
                  lazy-load
                />
              </div>
              <div class="nickname">{{ li.activity.author.n }}</div>
            </div>
            <div class="right">
              <div class="icon">
                <van-image
                  width="20px"
                  height="auto"
                  :src="li.activity.action_icon"
                  lazy-load
                />
              </div>
              <div class="text">{{ li.activity.action_title }}</div>
            </div>
          </div>
        </div>
        <div class="list" v-else @click="toNote(li.note)">
          <img :src="li.note.image_u" alt="" />
          <div class="title">{{ li.note.title }}</div>
          <div class="bottom">
            <div class="left">
              <div class="avatar">
                <van-image
                  width="20px"
                  height="20px"
                  round
                  :src="li.note.author.p"
                  lazy-load
                />
              </div>
              <div class="nickname">{{ li.note.author.n }}</div>
            </div>
            <div class="right">
              <div class="icon">
                <van-icon name="like-o" size="20" />
              </div>
              <div class="text">{{ li.note.like_count }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// import NoteLists from "@/components/NoteLists.vue";
import { storeToRefs } from "pinia";
import { useDouguoStore } from "@/stores/douguo";
import { ref, type Ref } from "vue";
import router from "@/router";

const douguo = useDouguoStore();

const { homeNote } = storeToRefs(douguo);
const { updataHomeNote } = douguo;

updataHomeNote();

const toActivity = (url: string) => {
  console.log("toActivity", url);
};

const toNote = (e) => {
  // console.log("toNote", e);
  router.push({
    path: "/noteDetail",
    query: { id: e.id, user_id: e.author.id },
  });
};
</script>

<style lang="scss" scoped>
.homeNote {
  .noteTopicBox {
    display: flex;
    flex-direction: column;
    overflow-x: scroll;

    .noteTopics {
      display: flex;
      flex-wrap: nowrap;
      align-items: center;
      margin-bottom: 10px;
      margin-left: 10px;

      .noteTopic {
        padding: 0 15px;
        height: 40px;
        border-radius: 20px;
        background-color: #f4f9fa;
        flex-shrink: 0;
        display: flex;
        align-items: center;
        margin-right: 10px;

        .icon {
          width: 12px;
          margin-right: 5px;
        }

        .name {
          font-size: 13px;
          font-weight: bold;
          color: #4b9697;
        }
      }

      .isLast {
        border: 1px solid #4b9697;
      }
    }
  }

  .noteLists {
    // padding: 0 10px;
    padding-left: 10px;
    background-color: #fafafb;

    .noteList {
      display: flex;
      width: 172px;
      border-radius: 10px;
      background-color: white;
      margin-bottom: 10px;
      margin-right: 10px;

      img {
        width: 172px;
        border-radius: 10px 10px 0 0;
      }

      .list {
        display: flex;
        flex-direction: column;
        margin-top: 0;

        .title {
          font-size: 15px;
          padding: 0 5px;
          margin-top: 10px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .bottom {
          padding: 0 5px;
          margin-top: 10px;
          margin-bottom: 8px;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .left {
            display: flex;
            align-items: center;

            .avatar {
              width: 20px;
              height: 20px;
              margin-right: 5px;
            }

            .nickname {
              width: 78px;
              font-size: 13px;
              color: #999;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }

          .right {
            display: flex;
            align-items: center;

            .icon {
              width: 20px;
              height: 20px;
              margin-right: 5px;
            }

            .text {
              font-size: 14px;
              color: #999;
            }
          }
        }
      }
    }

    .noteList:nth-of-type(2n) {
      margin-right: 0;
    }
  }
}
</style>
